const { query } = require("./mysql");

const addProductSql = async ({
  productData,
  userId,
  createTime,
  updateTime,
  aboutId,
}) => {
  const sql = `INSERT INTO product (id, productData, userId, createTime, updateTime, aboutId) VALUES (NULL, ?, ?, ?, ?, ?);`;
  const resp = await query(sql, [
    productData,
    userId,
    createTime,
    updateTime,
    aboutId,
  ]);
  return resp;
};

const editProductSql = async ({
  id,
  productData
}) => {
  const sql = `UPDATE product SET productData=?,updateTime=? WHERE id=?`;
  const resp = await query(sql, [
    productData,
    new Date().getTime(),
    id,
  ]);
  return resp;
};

const queryProductsById = async (id) => {
  const sql = 'select * from product where userId = ?'
  const result = await query(sql, [id])
  return result
}

const getProductById = async id => {
  const sql = 'select * from product where id = ?'
  const result = await query(sql, [id])
  return result
}


const delProductById = async id => {
  const sql = 'DELETE FROM `product` WHERE id = ?'
  const result = await query(sql, [id])
  return result
}


module.exports = { addProductSql, editProductSql, queryProductsById, getProductById, delProductById };
